package com.shenglue.ssi.dao;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.shenglue.ssi.domain.BuyInSettle;


public class BuyInSettleDaoImpl extends SqlMapClientDaoSupport implements BuyInSettleDao {

	/**
	 * 查询某入库单的 所有入库结账单信息
	 */
	public List<BuyInSettle> selectAllBuyInSettle( int buyInId ) {
		@SuppressWarnings("unchecked")
		List<BuyInSettle> list = this.getSqlMapClientTemplate().queryForList(
				"selectAllBuyInSettle", buyInId );

		if (list != null && list.size() > 0) {
			return list;
		} else {
			return null;
		}
	}

	/**
	 * 增加入库结账单记录
	 */
	public boolean saveBuyInSettle( BuyInSettle buyInSettle ) {

		try {
			Object oRtn = this.getSqlMapClientTemplate().insert( "insertBuyInSettle", buyInSettle );
			// 已改正 来自网上的写法。已有文章讨论

			//插入成功 返回true
			return true;
		}
		catch ( DataAccessException e ) {
			//失败 返回false
			return false;
		}
	}

	/**
	 * 删除入库结账单记录
	 */
	public boolean deleteBuyInSettle( int buyInSettleId ) {
		int flag= this.getSqlMapClientTemplate().delete("deleteBuyInSettleById", buyInSettleId);
		if( flag > 0 ) {
			return true;
		}
		return false;
	}

	/**
	 * 修改入库结账单记录
	 */
	public boolean updateBuyInSettle( BuyInSettle buyInSettle ) {
		int flag = this.getSqlMapClientTemplate().update("updateBuyInSettle", buyInSettle);
		if( flag > 0 ) {
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 为修改，根据 Id 查一条 入库结账单记录
	 */
	public BuyInSettle getOneBuyInSettleForUpdate( int buyInSettleId ) {
		Object oBuyInSettle = this.getSqlMapClientTemplate().
		queryForObject( "getOneBuyInSettleForUpdate", buyInSettleId );

		BuyInSettle buyInSettle = (BuyInSettle)oBuyInSettle;

		return buyInSettle;
	}

	/**
	 * 为计算结清情况，根据 buyInMainId 查一条 入库单主表 需要结账的额度（订单的总额）
	 */
	public float getOneBuyInMainNeedSettle( int buyInMainId ){
		Object oNeedSettle = this.getSqlMapClientTemplate().
		queryForObject( "getOneBuyInMainNeedSettle", buyInMainId );
		
		return (Float)oNeedSettle;
	}

	/**
	 * 为计算结清情况，根据 buyInMainId 查一条 入库单主表 已经结账的额度（结账单的总额）
	 */
	public float getOneBuyInMainHasSettle( int buyInMainId ){
		Object oHasSettle = this.getSqlMapClientTemplate().
		queryForObject( "getOneBuyInMainHasSettle", buyInMainId );
		
		return (Float)oHasSettle;
	}
}
